﻿using Infrastructure.Dao;
using SqlSugar;
using SyntacticSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels.DropGroup;
using DormMS.Model.ViewModels;

namespace DormMS.Service
{
    public class DropGroupService : IDropGroupService
    {
        private SqlSugarClient db;

        public IEnumerable<V_DropGroup> GetDropGroupList(DropGroupSearchModel search, ref int total)
        {
            using (db = DbConfig.GetInstance())
            {

                search.OrderByFields = search.OrderByFields;

                var list = db.Queryable<Model.DropGroup>()
                    .Where(m => m.Title.Contains(search.Keywords))
                    .OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total)
                    .Select(s => new V_DropGroup()
                    {
                        IsDeleted = s.IsDeleted,
                        BeginTime = s.BeginTime,
                        CreateTime = s.CreateTime,
                        DropGroupID = s.DropGroupID,
                        DropGroupTime = s.DropGroupTime,
                        DropPrice = s.DropPrice,
                        EndTime = s.EndTime,
                        FloorPrice = s.FloorPrice,
                        IsFree = s.IsFree,
                        OriginalPrice = s.OriginalPrice,
                        Remark = s.Remark,
                        SpecificationsID = s.SpecificationsID,
                        StartingPrice = s.StartingPrice,
                        State = s.State,
                        Title = s.Title,
                        TeamCount = db.Queryable<GroupHead>().Where(t => t.DropGroupID == s.DropGroupID).Count()
                    }).ToList();
                return list;
            }
        }


        public List<V_DropGroup> GetDropGroupListAll()
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";
                string sql =
                    $@"select t3.Name ProductName,t3.Id ProductID,t2.SpecNames SpecName,t3.ImgUrl ProductImgUrl,t3.Description ProductDescription,m.*,(select count(1) from GroupHead g where g.DropGroupID=m.DropGroupID) TeamCount from DropGroup m
                left join Product_Spec t2 on m.SpecificationsID = t2.Id left join Product t3 on t2.ProductSku=t3.Sku where  m.BeginTime<'" + DateTime.Now + "' and m.EndTime>'" + DateTime.Now + "' and state=0  and m.IsDeleted=0";
                var list = db.SqlQuery<V_DropGroup>(sql).OrderBy("CreateTime", OrderByType.Desc).ToList();
                return list;
            }
        }

        /// <summary>
        /// 更新活动状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateDropGroupState(int Id, int state)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.DropGroup>(new { state = state }, m => m.DropGroupID == Id);
            }
        }


        /// <summary>
        /// 删除活动
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool DelDropGroup(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.DropGroup>(new { IsDeleted = true }, m => m.DropGroupID == Id);
            }
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public V_DropGroup GetVDropGroupInfo(int Id)
        {
            V_DropGroup v_dropgroup = null;
            using (db = DbConfig.GetInstance())
            {
                string sql =
                    $@"select t3.Name ProductName,t3.Id ProductID,t2.SpecNames SpecName,t3.Description ProductDescription,t3.Detail ProductDetail,t3.ImgUrls ProductImgUrls,(select count(1) from GroupHead g where g.DropGroupID=t1.DropGroupID) TeamCount,t1.* from DropGroup t1
                inner join Product_Spec t2 on t1.SpecificationsID = t2.Id inner join Product t3 on t2.ProductSku=t3.Sku where t1.DropGroupID={
                        Id} and t1.isdeleted=0 and t2.isdeleted=0 and t3.isdeleted=0 and t3.IsDisplay=1";
                var dt = db.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    v_dropgroup = dt.TryToList<V_DropGroup>()[0];
                }
                return v_dropgroup;
            }

        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <returns></returns>
        public V_DropGroup GetGropGroupTop1()
        {
            V_DropGroup v_dropgroup = null;
            using (db = DbConfig.GetInstance())
            {
                string sql =
                    $@"select top 1 t3.Name ProductName,t3.Id ProductID,t2.SpecNames SpecName,t3.ImgUrl ProductImgUrl,t1.* from DropGroup t1
                inner join Product_Spec t2 on t1.SpecificationsID = t2.Id inner join Product t3 on t2.ProductSku=t3.Sku where  t1.BeginTime<'" + DateTime.Now + "' and t1.EndTime>'" + DateTime.Now + "' and state=0 and t1.IsDeleted=0 and t2.isdeleted=0 and t3.isdeleted=0 and t3.IsDisplay=1 order by t1.CreateTime desc";
                var dt = db.GetDataTable(sql);
                if (dt.Rows.Count > 0)
                {
                    v_dropgroup = dt.TryToList<V_DropGroup>()[0];
                }
                return v_dropgroup;
            }

        }


        /// <summary>
        /// 验证活动名称是否存在
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool CheckTitle(int Id, string title)
        {
            bool success = false;
            using (db = DbConfig.GetInstance())
            {
                var list = db.Queryable<Model.DropGroup>().Where(t => t.DropGroupID != Id && t.Title == title);
                if (list.Count() == 0)
                {
                    success = true;
                }
                return success;
            }
        }

        /// <summary>
        /// 获取活动信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.DropGroup GetDropGroupInfo(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Model.DropGroup>().SingleOrDefault(t => t.DropGroupID == Id);
                return model;
            }
        }

        /// <summary>
        /// 修改活动
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditDropGroup(Model.DropGroup model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Model.DropGroup>(model);
            }
        }


        /// <summary>
        /// 增加活动
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddDropGroup(Model.DropGroup model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Insert<Model.DropGroup>(model).ObjToInt();
            }
        }

        #region 降拼团
        public GroupHead FindGroupHead(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Queryable<GroupHead>().SingleOrDefault(t => t.GroupHeadID == Id);
            }
        }


        #endregion

    }
}
